Type.registerNamespace("Insp");
Type.registerNamespace("Insp.UI");

Insp.UI.PopSearchList = function(element)
{
    Insp.UI.PopSearchList.initializeBase(this,[element]);
    this._feedSuccessDelegate = null;
    this._feedFailureDelegate = null;
    this._searchDelegate = null;
    this._popSearchURL = "";
    this._searchURL = "";
    this._target = "";
    this._itemCount = 6;
    this._showSearchBox = true;
}

Insp.UI.PopSearchList.prototype = 
{

    get_popSearchURL:function(){return this._popSearchURL;},
    set_popSearchURL:function(value){this._popSearchURL = value;this.raisePropertyChanged('popSearchURL');},
    
    get_searchURL:function(){return this._searchURL;},
    set_searchURL:function(value){this._searchURL = value;this.raisePropertyChanged('searchURL');},
    
    get_itemCount:function(){return this._itemCount;},
    set_itemCount:function(value){this._itemCount = value;this.raisePropertyChanged('itemCount');},
    
    get_showSearchBox:function(){return this._showSearchBox;},
    set_showSearchBox:function(value){this._showSearchBox = value;this.raisePropertyChanged('showSearchBox');},
    
    get_target:function(){return this._target;},
    set_target:function(value){this._target = value;this.raisePropertyChanged('target');},
    
    initialize: function()
    {
        Insp.UI.PopSearchList.callBaseMethod(this,"initialize");
        var e = this.get_element();
        Sys.UI.DomElement.addCssClass(e, "ptModPopSearch");
        
        var sb = $get("SearchBoxCell",this.get_element());
        if(this.get_showSearchBox())sb.style.display="";
        else sb.style.display="none";
        
        if(this._feedSuccessDelegate === null)
            this._feedSuccessDelegate = Function.createDelegate(this, this._feedSuccess);
            
        if(this._feedFailureDelegate === null)
            this._feedFailureDelegate = Function.createDelegate(this, this._feedFailure);
            
        if(this._searchDelegate === null)
            this._searchDelegate = Function.createDelegate(this, this._searchHandler);
        $addHandler($get('SearchBtn',this.get_element()),'click',this._searchDelegate);
     },
    dispose: function()
    {
        $removeHandler($get('SearchBtn',this.get_element()),'click',this._searchDelegate);
        if(this._searchDelegate) delete this._searchDelegate;     
        if(this._feedSuccessDelegate) delete this._feedSuccessDelegate;     
        if(this._feedFailureDelegate) delete this._feedFailureDelegate;     
       Insp.UI.PopSearchList.callBaseMethod(this,"dispose");   
    },
    updated: function()
    {
        Portal.ClientRssSvc.GetDetails(this.get_popSearchURL(), 1,6,this._feedSuccessDelegate,this._feedFailureDelegate);
    },
    _feedSuccess:function(results)
    {
        this._populateList(results);
    },
    _feedFailure:function()
    {
        
    },
    _populateList:function(results)
    {
        for(var i=0;i<results.length;i++)
        {
            var oa = results[i].ArtHeadline;
            this._addWordToList(oa,i);
        }
    },
    _addWordToList:function(word,idx)
    {
        var klist = $get("KeywordList",this.get_element());
        var li = document.createElement('li');
        li.innerHTML = '<span class="bullet"><small>&#9632;&nbsp;</small></span><a target="' + this.get_target() + '" href="'+this.get_searchURL().replace(/\[SearchItem\]/ig,word)+'" onclick="LogClient(\'108\',\'portalmodule\');">'+word+'</a>';
        klist.appendChild(li);
    },
    _searchHandler:function(evt)
    {
        var searchText = $get('SearchTxt',this.get_element()).value;
        var errorDiv = $get('errorDiv',this.get_element());
        searchText = searchText.trim();
        evt.preventDefault();
        if(!searchText.length == 0)
        {      
              var modSearchTxt='';
              for(j=0;j<searchText.length;j++)
              {
                modSearchTxt+=SpecialCharacter(searchText.substring(j,j+1));
              }
              var finalUrl= this.get_searchURL().replace(/\[SearchItem\]/ig,modSearchTxt);
              window.open(finalUrl,this.get_target());
              errorDiv.style.display = "none";
              return true;             
        }
        else
        {
            errorDiv.innerHTML = "<span style=\"display: inline;\">Please enter valid values.Value should not be empty.</span>";
            errorDiv.style.display = "";
            return false;
        }
    }
}
Insp.UI.PopSearchList.registerClass('Insp.UI.PopSearchList',Sys.UI.Control);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

Type.registerNamespace("Insp");
Type.registerNamespace("Insp.UI");

Insp.UI.Widget = function(element)
{
    Insp.UI.Widget.initializeBase(this,[element]);
    this._url="";
    this._jsPrototype="";
    
    this._callbackDelegate = null;
    this._errorDelegate = null;
    this._jsExecDelegate = null;
}

Insp.UI.Widget.prototype = {
    get_url: function(){return this._url;},
    set_url: function(value){this._url = value;this.raisePropertyChanged('url');},
    
    get_jsPrototype: function(){return this._jsPrototype;},
    set_jsPrototype: function(value){this._jsPrototype = value;this.raisePropertyChanged('jsPrototype');},

    dispose: function()
    {
        if (this._callbackDelegate)delete this._callbackDelegate;
        if (this._errorDelegate)delete this._errorDelegate;
        if (this._jsExecDelegate)delete this._jsExecDelegate;
		Insp.UI.Widget.callBaseMethod(this,'dispose');
    },
    initialize: function()
    {
        Insp.UI.Widget.callBaseMethod(this,'initialize');
		this.addCssClass("ptModWidgt");
		this.addCssClass("moduleloading");
	
    	if (this._callbackDelegate === null) {
            this._callbackDelegate = Function.createDelegate(this, this.callbackHandler);
        }
        if (this._errorDelegate === null) {
            this._errorDelegate = Function.createDelegate(this, this._errorHandler);
        }
    	if (this._jsExecDelegate === null) {
            this._jsExecDelegate = Function.createDelegate(this, this._jsExecHandler);
        }
    },
    updated: function()
    {
        Portal.WidgetSvc.a(this.get_url(),this._callbackDelegate);
        Insp.UI.Widget.callBaseMethod(this,'updated');
    },
    callbackHandler: function(results)
    {
		if(results !== '')
		{
			this.removeCssClass("moduleloading");
			this.get_element().innerHTML = results;
			var d = this.get_element().getElementsByTagName("script");
			var t=d.length;
			for (var i=0;i<t;i++){
				var newScript = document.createElement('script');
				newScript.type = "text/javascript";
				newScript.text = d[i].text;
				if (d[i].src != '')
					newScript.src = d[i].src;
				this.get_element().appendChild (newScript);
			}
	        
			if(this.get_jsPrototype().length > 0)
			{
				window.setTimeout(this._jsExecDelegate,1000);
			}
		}else
			this._errorHandler();
    },
    _errorHandler: function(results)
    {
		this.removeCssClass("moduleloading");
		this.get_element().innerHTML = "<div style='margin:0;padding:1em 0;text-align:center;' class='feedErrorMessage'>"+widgetErrStr+"</div>";
    },
    _jsExecHandler: function()
    {
        obj = eval(this.get_jsPrototype());
        if(obj)obj.Execute(this.get_element());
    }
}
Insp.UI.Widget.registerClass('Insp.UI.Widget',Insp.UI.ModuleContentCtl);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
